﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;

namespace Hsp.Auto.Common.Data
{
    /// <summary>
    /// 获取数据特性扩展方法
    /// </summary>
    public static class AttributeExtend
    {
        /// <summary>
        /// 获取显示特性扩展方法
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static string GetDisplayName(this PropertyInfo property)
        {
            if (property.IsDefined(typeof(DisplayNameAttribute), true))
            {
                DisplayNameAttribute attribute = (DisplayNameAttribute)property.GetCustomAttribute(typeof(DisplayNameAttribute), true);
                return attribute.DisplayName;
            }
            else
            {
                return property.Name;
            }
        }

        /// <summary>
        /// 获取表名特性扩展方法
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static string GetTableName(this PropertyInfo property)
        {
            if (property.IsDefined(typeof(TableAttribute), true))
            {
                TableAttribute attribute = (TableAttribute)property.GetCustomAttribute(typeof(TableAttribute), true);
                return attribute.Name;
            }
            else
            {
                return property.Name;
            }
        }

        /// <summary>
        /// 获取表名
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetTableName(this Type type)
        {
            if (type.IsDefined(typeof(TableAttribute), true))
            {
                TableAttribute attribute = (TableAttribute)type.GetCustomAttribute(typeof(TableAttribute), true);
                return attribute.Name;
            }
            else
            {
                return type.Name;
            }
        }

        /// <summary>
        /// 获取表名
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        //public static string GetTableName(Type type)
        //{
        //    dynamic tableAttr = type!.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute")!;
        //    var name = tableAttr != null ? (string)tableAttr.Name : type.Name;
        //    return name;
        //}

        /// <summary>
        /// 获取列名特性扩展方法
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static string GetColumnName(this PropertyInfo property)
        {
            if (property.IsDefined(typeof(ColumnAttribute), true))
            {
                ColumnAttribute attribute = (ColumnAttribute)property.GetCustomAttribute(typeof(ColumnAttribute), true);
                return attribute.Name;
            }
            else
            {
                return property.Name;
            }
        }

        /// <summary>
        /// 获取列名
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetColumnName(this Type type)
        {
            if (type.IsDefined(typeof(ColumnAttribute), true))
            {
                ColumnAttribute attribute = (ColumnAttribute)type.GetCustomAttribute(typeof(ColumnAttribute), true);
                return attribute.Name;
            }
            else
            {
                return type.Name;
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        //public static string GetColumnName(Type type)
        //{
        //    dynamic columnAttr = type!.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "ColumnAttribute")!;
        //    var name = columnAttr != null ? (string)columnAttr.Name : type.Name;
        //    return name;
        //}

        /// <summary>
        /// 是否有有效数据库字段
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static bool IsValidSqlField(this PropertyInfo property)
        {
            if (!property.PropertyType.IsPublic || !property.CanWrite) return false;
            if (property.PropertyType.IsInterface) return false;
            if (!property.PropertyType.IsSerializable) return false;
            return true;
        }


        /// <summary>
        /// 获获取主键特性扩展方法(使用Key属性)
        /// </summary>
        /// <param name="type">属性类型</param>
        /// <returns></returns>
        public static string GetPrimaryKey(this Type type)
        {
            if (type.IsDefined(typeof(TableAttribute), false))
            {
                PropertyInfo[] properties = type.GetProperties();
                foreach (var property in properties)
                {
                    if (property.IsDefined(typeof(ColumnAttribute), true))
                    {
                        return property.Name;

                        //foreach (ColumnAttribute column in property.GetCustomAttributes(typeof(ColumnAttribute), false))
                        //{
                        //    if (column.PrimaryKey)
                        //    {
                        //        return property.Name;
                        //    }
                        //}
                    }
                }
            }

            // 如果属性特性没有定义主键，则返回默认主键名称，但是，可能是错的
            return "Id";

        }
    }
}
